package com.md.api.common.utils;

import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class AsiaPayUtil {




    private static String merchant_token = "5dc72bd5-a426-4657-820b-046fd7428ce4";
    private static String secret = "b7379bf3-8f77-4c4e-a4ed-fe37ab1084e1";

    public static void pay(String orderNo){
        try {
//            StringBuilder param= new StringBuilder();
//            param.append("merchant_reference="+orderNo);
//            param.append("&currency="+"HKD");
//            param.append("&amount="+100.00);
//            param.append("&customer_ip="+"123.123.123.123");
//            param.append("&customer_name="+"Someone Buying");
//            param.append("&customer_address="+"1, Bay Street");
//            param.append("&customer_phone="+"0123123123");
//            param.append("&customer_email="+"someone@gmail.com");
//            param.append("&customer_country="+"US");
//            param.append("&return_url="+URLEncoder.encode("https://demo.shop.com/payment/return","UTF-8"));
//            param.append("&network="+"Alipay");
//            param.append("&subject="+"IphoneX");


            Map<String, Object> reqMap= new HashMap<>();
            reqMap.put("merchant_reference",orderNo);
            reqMap.put("currency","HKD");
            reqMap.put("amount","100.00");
            reqMap.put("customer_ip","123.123.123.123");
            reqMap.put("customer_first_name","John");
            reqMap.put("customer_last_name","Doe");
            reqMap.put("customer_address","1, Bay Street");
            reqMap.put("customer_phone","0123123123");
            reqMap.put("customer_email","someone@gmail.com");
            reqMap.put("customer_state","NY");
            reqMap.put("customer_country","US");
            reqMap.put("return_url","https://demo.shop.com/payment/return");
            reqMap.put("network","Alipay");
            reqMap.put("subject","IphoneX");
//            param.append("&return_url="+URLEncoder.encode("http://114.116.252.181:8081/pay/rechargeNotify","UTF-8"));
//            reqMap.put("customer_address","重庆");
//            reqMap.put("customer_state","NY");
//            reqMap.put("customer_postal_code","500235");
//            reqMap.put("return_url","http://114.116.252.181:8081/pay/rechargeNotify");


            String sign = SHA512Util.encode(ksort(reqMap)+secret);

//            param.append("&sign="+sign);
            reqMap.put("sign",sign);
            String resultStr = HttpUtil.doPost("https://payment.pa-sys.com/app/page/"+merchant_token,reqMap);
            System.out.println(resultStr);

        }catch (Exception e ){
            e.printStackTrace();
        }
    }

    public static String ksort(Map<String, Object> map)  {

        String sb = "";
        String[] key = new String[map.size()];
        int index = 0;
        for (String k : map.keySet()) {
            key[index] = k;
            index++;
        }
        Arrays.sort(key);
        for (String s : key) {
            sb += s + "=" + map.get(s) + "&";
        }
        sb = sb.substring(0, sb.length() - 1);
        // 将得到的字符串进行处理得到目标格式的字符串
        try {
            sb = URLEncoder.encode(sb, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }// 使用常见的UTF-8编码
        sb = sb.replace("%3D", "=").replace("%26", "&");

        System.out.println(sb);
        return sb;
    }

    public static void main(String[] args) {
        AsiaPayUtil.pay("1234567890");
    }
}
